package com.hw.ui.upload_data;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.hw.R;
import com.hw.base.activity.BaseActivity;
import com.hw.bean.AbRelationBean;
import com.hw.dialog.ChooseDialog;

import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UploadActivity extends BaseActivity<UploadModel, UploadView, UploadPresent> implements UploadView{
    private String tag = "UploadActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.btproduce)
    Button btproduce;
    RelationAdapter relationAdapter;
    List<AbRelationBean> list = new ArrayList<AbRelationBean>();
    int index=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        ButterKnife.bind(this);
        tvTitle.setText("上传数据");
        View headView = getLayoutInflater().inflate(R.layout.list_rhead, null);
        listview.addHeaderView(headView);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(index==position-1)
                {
                    index=-1;
                }
                else
                {
                    index=position-1;
                }
                relationAdapter.setSelectedPosition(index);
                relationAdapter.notifyDataSetChanged();
            }
        });

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                ChooseDialog nDialog = new ChooseDialog(UploadActivity.this, "是否删除此箱数码", R.style.Dialog1, new ChooseDialog.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        presenter.deleteCase(list.get(position-1).getPid(),list.get(position-1).getBatch(),list.get(position-1).getCasec());
                    }
                });
                WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                nDialog.setCanceledOnTouchOutside(false);
                nDialog.show();
                return true;
            }
        });
        presenter.showlist();
    }

    @Override
    public UploadModel createModel() {
        return new UploadModelImpl(this);
    }

    @Override
    public UploadView createView() {
        return this;
    }

    @Override
    public UploadPresent createPresenter() {
        return new UploadPresent();
    }

    @Override
    public void showList(List<AbRelationBean> list) {
        index=-1;
        this.list = list;
        relationAdapter = new RelationAdapter(this,list);
        listview.setAdapter(relationAdapter);
    }

    @OnClick({R.id.iv_title_left, R.id.btproduce})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.btproduce:
                if(list.size()==0)
                {
                    showMsg("当前没有可供上传数据");
                    return;
                }
                presenter.uploadlist();
                break;
        }
    }
}
